package com.tao.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tao.ai.entity.Course;
import com.tao.ai.entity.Coursestudent;
import com.tao.ai.entity.Courseteacher;
import com.tao.ai.entity.Student;
import com.tao.ai.mapper.CourseMapper;
import com.tao.ai.mapper.CourseStudentMapper;
import com.tao.ai.mapper.CourseTeacherMapper;
import com.tao.ai.mapper.StudentMapper;
import com.tao.ai.service.CourseService;
import com.tao.ai.vo.CourseVo;
import com.tao.ai.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImp implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private CourseStudentMapper courseStudentMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public CourseVo<Course> getCourse(Integer page, Integer limit, String ppuser) {
        CourseVo tableVo=new CourseVo();
        tableVo.setCode(0);
        tableVo.setMsg("");

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("teacher_id",ppuser);

        List<Courseteacher> courseteacherList=courseTeacherMapper.selectList(wrapper);
        QueryWrapper wrapper1=new QueryWrapper();

        for(Courseteacher courseteacher:courseteacherList){
            wrapper1.eq("course_id",courseteacher.getCourseId());
            wrapper1.or();
        }

        tableVo.setCount(courseMapper.selectCount(wrapper1));

        IPage<Course> courseIPage=new Page<>(page,limit);
        IPage<Course> result=courseMapper.selectPage(courseIPage,wrapper1);
        List<Course> courseList=result.getRecords();
        tableVo.setData(courseList);
        return tableVo;
    }

    @Override
    public CourseVo<Course> getCourseName(String ppuser) {
        CourseVo tableVo=new CourseVo();
        tableVo.setCode(0);
        tableVo.setMsg("");

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("teacher_id",ppuser);

        List<Courseteacher> courseteacherList=courseTeacherMapper.selectList(wrapper);
        QueryWrapper wrapper1=new QueryWrapper();

        for(Courseteacher courseteacher:courseteacherList){
            wrapper1.eq("course_id",courseteacher.getCourseId());
            wrapper1.or();
        }

        tableVo.setCount(courseMapper.selectCount(wrapper1));
        List<Course> courseList=courseMapper.selectList(wrapper1);

        tableVo.setData(courseList);
        return tableVo;
    }

    @Override
    public CourseVo<Course> getCourseName2(String ppuser) {
        CourseVo tableVo=new CourseVo();
        tableVo.setCode(0);
        tableVo.setMsg("");

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("student_id",ppuser);

        List<Coursestudent> coursestudentList=courseStudentMapper.selectList(wrapper);
        QueryWrapper wrapper1=new QueryWrapper();

        for(Coursestudent coursestudent:coursestudentList){
            wrapper1.eq("course_id",coursestudent.getCourseId());
            wrapper1.or();
        }

        tableVo.setCount(courseMapper.selectCount(wrapper1));
        List<Course> courseList=courseMapper.selectList(wrapper1);

        tableVo.setData(courseList);
        return tableVo;
    }

    @Override
    public CourseVo<StudentVo> getStudentByCourse(Integer page, Integer limit, String courseName) {
        if (courseName==null) {
            CourseVo cVo=new CourseVo();
            cVo.setCode(0);
            cVo.setMsg("");
            cVo.setData(null);
            cVo.setCount(0);
            return cVo;
        }else {
            CourseVo cVo = new CourseVo();
            cVo.setCode(0);
            cVo.setMsg("");

            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("course_name", courseName);
            Integer courseId = courseMapper.selectOne(wrapper).getCourseId();
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("course_id", courseId);

            List<Coursestudent> courseStudentList = courseStudentMapper.selectList(wrapper1);

            QueryWrapper wrapper2 = new QueryWrapper();
            for (Coursestudent coursestudent : courseStudentList) {
                wrapper2.eq("student_id", coursestudent.getStudentId());
                wrapper2.or();
            }

            cVo.setCount(studentMapper.selectCount(wrapper2));

            IPage<Student> studentVoIPage = new Page<>(page, limit);
            IPage<Student> result = studentMapper.selectPage(studentVoIPage, wrapper2);
            List<Student> studentList = result.getRecords();

            List<StudentVo> studentVoList = new ArrayList<>();
            for(Student student:studentList){
                StudentVo studentVo=new StudentVo();
                BeanUtils.copyProperties(student,studentVo);

                QueryWrapper wrapper3 = new QueryWrapper();
                wrapper3.eq("student_id",student.getStudentId());
                wrapper3.eq("course_id",courseId);
                Integer sign=courseStudentMapper.selectOne(wrapper3).getSign();
                if(sign==0){
                    studentVo.setStudentSign("未签到");
                }else{
                    studentVo.setStudentSign("已签到");
                }

                studentVoList.add(studentVo);
            }

            cVo.setData(studentVoList);
            return cVo;
        }
    }
}
